<?php

namespace App\Admin\Controllers\Goods;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Models\Goods\CmcGoodsEvaluate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CmcGoodsEvaluateController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcGoodsEvaluate(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('goods_name','商品名称')->editable(true);
            $grid->column('goods_image','商品图片')->image($server=null,100,100);
            $grid->column('product_score','商品分数');
            $grid->column('nickname','昵称');
            $grid->column('avatar','头像')->image($server=null,100,100);
            $grid->column('again_content','追加评价内容')->editable(true);
            $grid->column('add_time');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // 倒序排序
            $grid->model()->orderBy('created_at', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->equal('goods_name','商品名称');
                $filter->equal('uid','用户ID');
                $filter->between('created_at','创建时间')->datetime();
                // 关联关系查询
                $filter->equal('is_reply','评论状态')->select(['0'=>'未回复','1'=>'回复']);
            });
            // 模糊搜索
            $grid->quickSearch(['nickname', 'goods_name']);
            $grid->column('is_reply', '评论状态')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'未回复',
                    1=>'已回复'
                ])
            );
            $grid->selector(function (Grid\Tools\Selector $selector){
                $selector->select('explain_type','时间',[
                    1=>'全部',
                    2=>'昨天',
                    3=>'最近7天',
                    4=>'最近30天',
                    5=>'本月',
                    6=>'本年'
                ]);
            });
            // 回收站
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(CmcGoodsEvaluate::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(CmcGoodsEvaluate::class));
                }
            });
            // 异步渲染功能
            $grid->async();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcGoodsEvaluate(), function (Show $show) {
            $show->field('id');
            $show->field('goods_id');
            $show->field('uid');
            $show->field('sku_id');
            $show->field('goods_name');
            $show->field('goods_image');
            $show->field('product_score');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('again_content');
            $show->field('explain_type');
            $show->field('is_show');
            $show->field('add_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcGoodsEvaluate(), function (Form $form) {
            $form->display('id')->rules('required');
            $form->text('goods_id','商品ID')->rules('required');
            $form->text('uid','用户ID')->rules('required');
            $form->text('sku_id','商品skuid')->rules('required');
            $form->text('goods_name','商品名称')->rules('required');
            $form->image('goods_image','商品图片')->rules('required');
            $form->text('product_score','商品分数')->rules('required');
            $form->text('nickname','评价人名称')->rules('required');
            $form->image('avatar','评价人头像')->rules('required');
            $form->text('again_content','追加评价内容')->minLength(10,'最少评论10个字')->maxLength(800,'最多评论800字')->rules('required');
            $form->text('explain_type','评价类型')->rules('required');
            $form->text('is_show','是否显示')->rules('required');
            $form->text('detail_content','1视频/2图片')->rules('required');
            $form->text('pid','评论层级')->rules('required');
            $form->text('look_num','浏览次数')->rules('required');
            $form->datetime('add_time','评价时间')->rules('required');

            $form->display('created_at');
            $form->display('updated_at');
            $form->confirm('您确定要提交表单吗？');
        });
    }
}
